package com.dp.mallchat.common.chat.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.dp.mallchat.common.chat.dao.GroupMemberDao;
import com.dp.mallchat.common.chat.dao.MessageDao;
import com.dp.mallchat.common.chat.dao.RoomFriendDao;
import com.dp.mallchat.common.chat.domain.entity.*;
import com.dp.mallchat.common.chat.domain.vo.req.SendMessageReq;
import com.dp.mallchat.common.chat.mapper.MessageMapper;
import com.dp.mallchat.common.chat.service.MessageService;
import com.dp.mallchat.common.chat.service.cache.RoomCache;
import com.dp.mallchat.common.chat.service.cache.RoomGroupCache;
import com.dp.mallchat.common.chat.service.strategy.AbstractMsgHandler;
import com.dp.mallchat.common.chat.service.strategy.MsgHandlerFactory;
import com.dp.mallchat.common.common.domain.enums.DeleteStatusEnum;
import com.dp.mallchat.common.common.event.SendMessageEvent;
import com.dp.mallchat.common.common.utils.AssertUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * @Author: dupeng
 * @CreateTime: 2024-06-27  19:28
 * @Description: TODO
 */
@Service
@Slf4j
public class MessageServiceImpl extends ServiceImpl<MessageMapper, Message> implements MessageService {


    @Autowired
    private RoomFriendDao roomFriendDao;
    @Autowired
    private GroupMemberDao groupMemberDao;

    @Autowired
    private RoomCache roomCache;
    @Autowired
    private RoomGroupCache roomGroupCache;
    @Autowired
    private ApplicationEventPublisher applicationEventPublisher;

    @Override
    @Transactional
    public Long sendMessage(Long uid, SendMessageReq req) {
        //验证房间是否有效
        checkRoom(uid, req.getRoomId());
        //发送消息
        AbstractMsgHandler abstractMsgHandler = MsgHandlerFactory.getStrategyNoNull(req.getMsgType());
        Long msgId = abstractMsgHandler.checkAndSaveMsg(req, uid);
        //消息发送事件
        applicationEventPublisher.publishEvent(new SendMessageEvent(this, msgId));
        return msgId;
    }


    private void checkRoom(Long uid, Long roomId) {
        Room room = roomCache.get(roomId);
        AssertUtil.isNotEmpty(room, "房间不存在");

        //如果是热门房间，直接放行
        if (room.isHotRoom()) {
            return;
        }

        //单聊
        if (room.isFriendRoom()) {
            //发送者是否在房间内
            RoomFriend roomFriend = roomFriendDao.getByRoomId(roomId);
            AssertUtil.isNotEmpty(roomFriend, "房间不存在");
            AssertUtil.equal(DeleteStatusEnum.NORMAL.getStatus(), roomFriend.getStatus(), "您已经被对方拉黑");
            AssertUtil.isTrue(uid.equals(roomFriend.getUid1()) || uid.equals(roomFriend.getUid2()), "您已经被对方拉黑");

//            AssertUtil.equal(DeleteStatusEnum.NORMAL.getStatus(), roomFriend.getStatus(), "房间已禁用");
//            AssertUtil.isTrue(roomFriend.getUid1().equals(uid), "您不在此房间内");
//            AssertUtil.isTrue(roomFriend.getUid2().equals(uid), "您不在此房间内");
        }

        //群聊
        if (room.isGroupRoom()) {
            RoomGroup roomGroup = roomGroupCache.get(roomId);
            AssertUtil.isNotEmpty(roomGroup, "群聊不存在");
            //人员是否存在群聊
            GroupMember groupMember = groupMemberDao.getByGroupIdAndUid(roomGroup.getId(), uid);
            AssertUtil.isNotEmpty(groupMember, "您已被移除此群聊");
        }

    }
}
